package org.eae.eldf.dao.module.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.eae.eldf.dao.module.IModuleInfoDao;
import org.eae.eldf.dto.module.ModuleInfoQueryDto;
import org.eae.eldf.entity.module.ModuleInfo;
import org.eae.eldf.framework.dao.JdbcDaoImpl;
import org.eae.eldf.framework.pager.Pagination;
import org.springframework.stereotype.Repository;

@Repository
public class ModuleInfoDaoImpl extends JdbcDaoImpl implements IModuleInfoDao {
	/**
	 * 分页模糊查询
	 */
	@Override
	public Pagination<ModuleInfo> pageModuleList(Pagination<ModuleInfo> pager, ModuleInfoQueryDto dto) {

		String sql = "SELECT * FROM MODULE_INFO M WHERE M.MODULE_IS_DELETE ='0' ";
		List<Object> params = new ArrayList<Object>();
		if (null != dto) {
			if (StringUtils.isNotBlank(dto.getModuleName())) {
				sql += "AND M.MODULE_NAME LIKE ?  ";
				params.add("%" + dto.getModuleName() + "%");
			}
			if (StringUtils.isNotBlank(dto.getModuleCode())) {
				sql += "AND M.MODULE_CODE LIKE ?  ";
				params.add("%" + dto.getModuleCode() + "%");
			}

			if (StringUtils.isNotBlank(pager.getOrderBy()) && StringUtils.isNotBlank(pager.getSort())) {
				sql += " ORDER BY " + pager.getOrderBy().toUpperCase() + " " + pager.getSort().toUpperCase();
			} else {
				sql += " ORDER BY MODULE_INFO_ID ASC";
			}
		}
		return this.queryListByPage(pager, sql.toString(), params, ModuleInfo.class);
	}

	@Override
	public List<ModuleInfo> getCompanyHasModules(String companyCode) {
		if (companyCode != null && StringUtils.isNotBlank(companyCode)) {
			List<Object> params = new ArrayList<Object>();
				String sql="select * from module_info m where m.module_info_id in(select c.module_info_id from module_company c where company_code=?) and m.module_is_delete=0";
				params.add(companyCode);
				return this.queryBySQL(sql, params, ModuleInfo.class);
		}

		return null;
	}

	@Override
	public List<ModuleInfo> getCompanyNotHasModules(String companyCode) {

		if (companyCode != null && StringUtils.isNotBlank(companyCode)) {
			List<Object> params = new ArrayList<Object>();
			String sql="select * from module_info m where m.module_info_id not in(select c.module_info_id from module_company c where company_code=?) and m.module_is_delete=0";
			params.add(companyCode);
			return this.queryBySQL(sql, params, ModuleInfo.class);

		}
		return null;
	}

}
